package com.taomu.app.zongchouhuli.utils.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.utils.DpUtils;
import com.taomu.app.zongchouhuli.utils.adpter.SelectBankAdp;
import com.taomu.app.zongchouhuli.utils.entity.SelectBankEntity;

import java.util.ArrayList;
import java.util.List;

public class SelectBankDialog extends DialogFragment {

    private ImageView mCloseIm;

    private RecyclerView mRecy;

    private SelectBankAdp mAdp;

    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.dialog_select_bank, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initEvent();
    }

    private void initView(){
        mCloseIm = view.findViewById(R.id.dialog_select_bank_close_im);
        mAdp = new SelectBankAdp(getContext());
        mRecy = view.findViewById(R.id.dialog_select_bank_recy);
        mRecy.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecy.setAdapter(mAdp);
    }

    private List<SelectBankEntity> mData;

    private SelectBankEntity selectData;

    private void initData(){
        mAdp.setData(mData,selectData);
    }

    private void initEvent(){
        mCloseIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        this.setStyle(STYLE_NORMAL, R.style.bottom_up_dialog);
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = DpUtils.screnWidth();
        //todo:需要改成银行卡数量加1
        lp.height = DpUtils.dp2px(36*(mData.size()+1));
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setAttributes(lp);
        return dialog;
    }

    private SelectBankDialogListener listener;

    public void setSelectBankDialogListener(SelectBankDialogListener listener){
        this.listener = listener;
    }

    public interface SelectBankDialogListener{
        void onDismiss(SelectBankEntity entity);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if(listener != null){
            if(mAdp.getSelectData().size()>=1){
                listener.onDismiss(mAdp.getSelectData().get(0));
            }
        }
        super.onDismiss(dialog);
    }

    @Override
    public int show(FragmentTransaction transaction, String tag) {
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.add(this,tag).addToBackStack(null);
        return transaction.commitAllowingStateLoss();
    }

    public void setData(List<SelectBankEntity> mData, SelectBankEntity data){
        if(this.mData == null) {
            this.mData = mData;
        }
        this.selectData = data;
    }
}
